package com.ybc.chinese.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;
import com.ybc.chinese.R;
import com.ybc.chinese.utils.CommonUtil;
import com.ybc.chinese.utils.Contants;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * <p>反馈<p/>
 * Created by Yubch on 17/2/15.
 */
public class AboutActivity extends BaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_left)
    ImageView ivLeft;
    @Bind(R.id.tv_version)
    TextView tvVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        tvTitle.setText(getString(R.string.about));
        ivLeft.setImageResource(R.mipmap.ic_back);
        ivLeft.setVisibility(View.VISIBLE);
        tvVersion.setText(getString(R.string.about_version, CommonUtil.getVersionName(this)));
    }

    @OnClick(R.id.iv_left)
    public void left() {
        finish();
    }

    @OnClick(R.id.btn_update)
    public void update() {
        showRunningDialog(R.string.requesting);
        PgyUpdateManager.register(this, Contants.PROVIDE_PATH, new UpdateManagerListener() {
            @Override
            public void onNoUpdateAvailable() {
                dismissRunningDialog();
                CommonUtil.showToast(AboutActivity.this, "已经是最新的版本");
            }

            @Override
            public void onUpdateAvailable(String s) {
                dismissRunningDialog();
                // 将新版本信息封装到AppBean中
                final AppBean appBean = getAppBeanFromString(s);
                new AlertDialog.Builder(AboutActivity.this)
                        .setTitle("更新")
                        .setMessage(appBean.getReleaseNote())
                        .setPositiveButton("下载", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(
                                    DialogInterface dialog,
                                    int which) {
                                startDownloadTask(
                                        AboutActivity.this,
                                        appBean.getDownloadURL());
                            }
                        })
                        .setNegativeButton(
                                "取消",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        dialog.dismiss();
                                    }
                                })
                        .show();
            }
        });
    }
}
